using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using FormsGenerator.Xml;
using FormsGeneratorPlugins.IO;

namespace FormsGenerator
{
    public partial class FormResults : System.Web.UI.Page
    {
        private string _guid;
        private string _fileDataDelimiter = "##File|";
        private string _fileHanlderUrl = "GetFile.ashx?id={0}&filename={1}&fieldname={2}&isTemporary=false";
            
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["id"] == null || Request["id"].Equals(""))
            {
                LabelErrorMessage.Text = "id is empty. Please provide a form id!";
                LabelErrorMessage.Visible = true;
            }
            else
            {
                _guid = GUID;

                string sort = "";
                if (!IsPostBack)
                {
                    BindGridViewResults(sort);
                }

            }
        }

        protected string GUID
        {
            get
            {
                return Request["id"].Trim();
            }
        }

        private void BindGridViewResults(string sort)
        {
            FormResultsDBHandler dbHandler = new FormResultsDBHandler();
            DataTable results = dbHandler.GetResults(_guid);
            if (results.Rows.Count > 0)
            {
                Response.Write(results.DefaultView.Sort); 
                results.DefaultView.Sort = sort + SortDirection;                
            }
            GridViewResults.DataSource = results;
            GridViewResults.DataBind();
        }

        protected void GridViewResults_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                foreach (TableCell cell in e.Row.Cells)
                {
                    if (cell.Text.Contains(_fileDataDelimiter))
                    {
                        string[] files = cell.Text.Split(',');
                        cell.Text = "";                        
                        foreach (string fileData in files)
                        {
                            if (!string.IsNullOrEmpty(fileData))
                            {
                                string[] fileDataSegments = fileData.Split('|');
                                string fieldName = fileDataSegments[1];
                                string resultId = fileDataSegments[2];
                                string fileName = fileDataSegments[3];
                                HyperLink hyperLinkFile = new HyperLink();
                                hyperLinkFile.Text = fileName;
                                hyperLinkFile.NavigateUrl = string.Format(_fileHanlderUrl, resultId, fileName, fieldName);
                                
                                cell.Controls.Add(hyperLinkFile);
                                cell.Controls.Add(new HtmlGenericControl("br"));
                            }
                        }                        
                    }
                }
            }
        }

        protected void GridViewResults_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (PreviousSortExpression == e.SortExpression)
            {
                if (SortDirection == " DESC")
                {
                    SortDirection = "";
                }
                else
                {
                    SortDirection = " DESC";
                }
            }
            else
            {
                SortDirection = "";             
            }
            PreviousSortExpression = e.SortExpression;                
            BindGridViewResults(e.SortExpression);
        }


        protected string PreviousSortExpression
        {
            get
            {
                return ViewState["PreviousSortExpression"] as string ?? "";
            }
            set
            {
                ViewState["PreviousSortExpression"] = value;
            }
        }

        protected string SortDirection
        {
            get
            {
                return ViewState["SortDirection"] as string ?? "";
            }
            set
            {
                ViewState["SortDirection"] = value;
            }
        }
    }
}

